<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>头像管理 - 学生知识管理系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            color: #333;
        }

        .navbar {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            padding: 1rem 0;
            box-shadow: 0 2px 20px rgba(0, 0, 0, 0.1);
            position: sticky;
            top: 0;
            z-index: 1000;
        }

        .nav-container {
            max-width: 1200px;
            margin: 0 auto;
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 0 2rem;
        }

        .logo {
            font-size: 1.5rem;
            font-weight: bold;
            color: #667eea;
            text-decoration: none;
        }

        .nav-links {
            display: flex;
            list-style: none;
            gap: 2rem;
        }

        .nav-links a {
            text-decoration: none;
            color: #333;
            font-weight: 500;
            transition: color 0.3s ease;
        }

        .nav-links a:hover {
            color: #667eea;
        }

        .container {
            max-width: 1200px;
            margin: 2rem auto;
            padding: 0 2rem;
            display: flex;
            gap: 2rem;
        }

        .sidebar {
            width: 250px;
            background: white;
            border-radius: 15px;
            padding: 2rem;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            height: fit-content;
        }

        .profile-avatar {
            text-align: center;
            margin-bottom: 2rem;
        }

        .avatar {
            width: 80px;
            height: 80px;
            border-radius: 50%;
            background: linear-gradient(135deg, #667eea, #764ba2);
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 2rem;
            font-weight: bold;
            margin: 0 auto 1rem;
        }

        .sidebar-menu {
            list-style: none;
        }

        .sidebar-menu li {
            margin-bottom: 0.5rem;
        }

        .sidebar-menu a {
            display: block;
            padding: 0.75rem 1rem;
            text-decoration: none;
            color: #666;
            border-radius: 8px;
            transition: all 0.3s ease;
        }

        .sidebar-menu a:hover,
        .sidebar-menu a.active {
            background: linear-gradient(135deg, #667eea, #764ba2);
            color: white;
        }

        .main-content {
            flex: 1;
            background: white;
            border-radius: 15px;
            padding: 2rem;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }

        .page-header {
            margin-bottom: 2rem;
        }

        .page-title {
            font-size: 2rem;
            color: #333;
            margin-bottom: 0.5rem;
        }

        .page-subtitle {
            color: #666;
            font-size: 1.1rem;
        }

        .avatar-section {
            text-align: center;
            margin-bottom: 3rem;
        }

        .current-avatar {
            width: 150px;
            height: 150px;
            border-radius: 50%;
            background: linear-gradient(135deg, #667eea, #764ba2);
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 3rem;
            font-weight: bold;
            margin: 0 auto 1rem;
            border: 5px solid #f0f0f0;
        }

        .upload-section {
            background: #f8f9fa;
            border-radius: 10px;
            padding: 2rem;
            margin-bottom: 2rem;
        }

        .upload-area {
            border: 2px dashed #ddd;
            border-radius: 10px;
            padding: 3rem;
            text-align: center;
            transition: all 0.3s ease;
            cursor: pointer;
        }

        .upload-area:hover {
            border-color: #667eea;
            background: rgba(102, 126, 234, 0.05);
        }

        .upload-icon {
            font-size: 3rem;
            color: #ddd;
            margin-bottom: 1rem;
        }

        .upload-text {
            color: #666;
            font-size: 1.1rem;
            margin-bottom: 0.5rem;
        }

        .upload-hint {
            color: #999;
            font-size: 0.9rem;
        }

        .file-input {
            display: none;
        }

        .btn {
            background: linear-gradient(135deg, #667eea, #764ba2);
            color: white;
            border: none;
            padding: 0.75rem 2rem;
            border-radius: 25px;
            cursor: pointer;
            font-size: 1rem;
            font-weight: 500;
            transition: all 0.3s ease;
            text-decoration: none;
            display: inline-block;
        }

        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
        }

        .btn-secondary {
            background: #6c757d;
        }

        .btn-secondary:hover {
            box-shadow: 0 5px 15px rgba(108, 117, 125, 0.4);
        }

        .alert {
            padding: 1rem;
            border-radius: 8px;
            margin-bottom: 1rem;
        }

        .alert-success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .alert-error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .preview-section {
            display: none;
            text-align: center;
            margin-top: 2rem;
        }

        .preview-image {
            max-width: 200px;
            max-height: 200px;
            border-radius: 10px;
            margin-bottom: 1rem;
        }

        .action-buttons {
            display: flex;
            gap: 1rem;
            justify-content: center;
            margin-top: 2rem;
        }
    </style>
</head>
<body>
    <!-- 导航栏 -->
    <nav class="navbar">
        <div class="nav-container">
            <a href="/" class="logo">📚 学生知识管理系统</a>
            <ul class="nav-links">
                <li><a href="/">🏠 首页</a></li>
                <li><a href="/articles">📝 文章</a></li>
                <li><a href="/categories">📂 分类</a></li>
                <li><a href="/profile">👤 个人中心</a></li>
                <li><a href="/logout">🚪 退出</a></li>
            </ul>
        </div>
    </nav>

    <div class="container">
        <!-- 侧边栏 -->
        <aside class="sidebar">
            <div class="profile-avatar">
                <div class="avatar" th:if="${user.avatar == null}" th:text="${user.nickname != null ? user.nickname.substring(0,1) : user.username.substring(0,1)}">U</div>
                <img th:if="${user.avatar != null}" th:src="${user.avatar}" alt="用户头像" class="avatar" style="width: 60px; height: 60px; border-radius: 50%; object-fit: cover;">
                <h3 th:text="${user.nickname != null ? user.nickname : user.username}">用户名</h3>
            </div>
            <ul class="sidebar-menu">
                <li><a href="/profile">📝 个人资料</a></li>
                <li><a href="/profile/avatar" class="active">🖼️ 头像管理</a></li>
                <li><a href="/profile/points">⭐ 积分管理</a></li>
                <li><a href="/profile/articles">📚 我的文章</a></li>
                <li><a href="/profile/favorites">❤️ 我的收藏</a></li>
                <li><a href="/profile/follows">👥 我的关注</a></li>
                <li><a href="/profile/messages">💬 消息中心</a></li>
            </ul>
        </aside>

        <!-- 主要内容 -->
        <main class="main-content">
            <div class="page-header">
                <h1 class="page-title">🖼️ 头像管理</h1>
                <p class="page-subtitle">上传和管理您的个人头像</p>
            </div>

            <!-- 错误信息 -->
            <div th:if="${error}" class="alert alert-error">
                <span th:text="${error}"></span>
            </div>

            <!-- 成功信息 -->
            <div th:if="${success}" class="alert alert-success">
                <span th:text="${success}"></span>
            </div>

            <!-- 当前头像 -->
            <div class="avatar-section">
                <h3>当前头像</h3>
                <div class="current-avatar" th:if="${user.avatar == null}" 
                     th:text="${user.nickname != null ? user.nickname.substring(0,1) : user.username.substring(0,1)}">U</div>
                <img th:if="${user.avatar != null}" th:src="${user.avatar}" alt="用户头像" class="current-avatar" style="object-fit: cover;">
            </div>

            <!-- 上传区域 -->
            <div class="upload-section">
                <h3>上传新头像</h3>
                <form id="avatarForm" enctype="multipart/form-data">
                    <div class="upload-area" onclick="document.getElementById('avatarFile').click()">
                        <div class="upload-icon">📷</div>
                        <div class="upload-text">点击选择图片文件</div>
                        <div class="upload-hint">支持 JPG、PNG、GIF 格式，文件大小不超过 5MB</div>
                    </div>
                    <input type="file" id="avatarFile" name="file" class="file-input" accept="image/*" onchange="previewImage(this)">
                </form>

                <!-- 预览区域 -->
                <div id="previewSection" class="preview-section">
                    <h4>预览</h4>
                    <img id="previewImage" class="preview-image" alt="预览图片">
                    <div class="action-buttons">
                        <button type="button" class="btn" onclick="uploadAvatar()">上传头像</button>
                        <button type="button" class="btn btn-secondary" onclick="cancelPreview()">取消</button>
                    </div>
                </div>
            </div>
        </main>
    </div>

    <script>
        function previewImage(input) {
            if (input.files && input.files[0]) {
                const reader = new FileReader();
                reader.onload = function(e) {
                    document.getElementById('previewImage').src = e.target.result;
                    document.getElementById('previewSection').style.display = 'block';
                };
                reader.readAsDataURL(input.files[0]);
            }
        }

        function cancelPreview() {
            document.getElementById('avatarFile').value = '';
            document.getElementById('previewSection').style.display = 'none';
        }

        function uploadAvatar() {
            const fileInput = document.getElementById('avatarFile');
            if (!fileInput.files[0]) {
                alert('请选择要上传的图片文件');
                return;
            }

            const formData = new FormData();
            formData.append('file', fileInput.files[0]);

            fetch('/api/users/avatar', {
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    alert('头像上传成功！');
                    location.reload();
                } else {
                    alert('头像上传失败：' + data.message);
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('头像上传失败，请重试');
            });
        }
    </script>
</body>
</html>